package com.haliyoo.adhere.sqlite;

import java.io.ByteArrayOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.haliyoo.adhere.bean.UserBean;
import com.haliyoo.adhere.bean.WeiboBean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class DataHelper4Weibo {
	private static final String TAG = "DataHelper4Weibo";
    /// 数据库名称
    private static String DB_NAME = "adweibo.db";
    /// 数据库版本
    private static int DB_VERSION = 2;
    private SQLiteDatabase db;
    private SqliteHelper4Weibo dbHelper;
    
    public DataHelper4Weibo(Context context){
        dbHelper = new SqliteHelper4Weibo(context,DB_NAME, null, DB_VERSION);
        db  = dbHelper.getWritableDatabase();
    }
    
    public void close()
    {
        db.close();
        dbHelper.close();
    }
    
    /// 获取weibo表中所有记录
    public List<WeiboBean> select()
    {
        List<WeiboBean> weiboList = new ArrayList<WeiboBean>();
        Cursor cursor = db.query(SqliteHelper4Weibo.TB_NAME, null, null, null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast() && (cursor.getString(1)!= null)){
        	WeiboBean weiboBean = new WeiboBean();
        	if (cursor.getString(0) != null){
        		weiboBean.setId(cursor.getString(0));
        	}
        	if (cursor.getString(1) != null){
        		weiboBean.setUser(cursor.getString(1));
        	}
        	///头像
            byte[] profile = cursor.getBlob(2);
            if (profile != null){
            	weiboBean.setProfile(BitmapFactory.decodeByteArray(profile,0,profile.length));
            }
        	
        	weiboBean.setWeiboId(cursor.getLong(3));
        	if (cursor.getString(4) != null){
        		weiboBean.setText(cursor.getString(4));
        	}
        	
        	weiboBean.setExistImage(cursor.getShort(5)==1? true: false);
        	
        	///图片
        	byte[] icon = cursor.getBlob(6);
            if (icon != null){
            	int len = icon.length;
            	Bitmap bitmap = BitmapFactory.decodeByteArray(icon,0,len);
            	weiboBean.setIcon(bitmap);
            }

            ///是否存在GPS
            weiboBean.setExistLocation(cursor.getShort(7)==1? true: false);

            ///GPS
            if (weiboBean.getExistLocation()) {
            	weiboBean.setLatitude(cursor.getDouble(8));
            	weiboBean.setLongitude(cursor.getDouble(9));
            }
            
        	///创建日期
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	try {
        		if (cursor.getString(10) != null){
        			weiboBean.setTime(sdf.parse(cursor.getString(10)));
        		}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        	weiboList.add(weiboBean);
            cursor.moveToNext();
        }
        cursor.close();
        
        return weiboList;
    }

    /// 更新weibo表的记录
    public int update(WeiboBean weibo)
    {
        ContentValues values = new ContentValues();

        values.put(WeiboBean.USER, weibo.getUser());
        
        ///头像
        if (weibo.getProfile() != null){
        	ByteArrayOutputStream profile = new ByteArrayOutputStream(); 
	        weibo.getProfile().compress(Bitmap.CompressFormat.JPEG, 50, profile);  
	        values.put(WeiboBean.PROFILE, profile.toByteArray());
        }

        if (weibo.getWeiboId() != 0){
        	values.put(WeiboBean.WEIBOID, weibo.getWeiboId());
        }
        if (weibo.getText() != null){
        	values.put(WeiboBean.TEXT, weibo.getText());
        }
        
        values.put(WeiboBean.EXISTICON, weibo.getExistImage()? 1: 0);
        
        ///图片
        if (weibo.getIcon() != null){
	        ByteArrayOutputStream icon = new ByteArrayOutputStream(); 
	        weibo.getIcon().compress(Bitmap.CompressFormat.JPEG, 50, icon);  
	        values.put(WeiboBean.ICON, icon.toByteArray());
        }

        values.put(WeiboBean.EXISTLOCATION, weibo.getExistLocation()? 1: 0);
        
        ///GPS
        if (weibo.getExistLocation()) {
	        values.put(WeiboBean.LATITUDE, weibo.getLatitude());
	        values.put(WeiboBean.LONGITUDE, weibo.getLongitude());
        }
        
        ///创建日期
        if (weibo.getTime() != null){
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        values.put(WeiboBean.CREATEAT, sdf.format(weibo.getTime()));
        }

        int id = db.update(SqliteHelper4Weibo.TB_NAME, values, WeiboBean.ID + "=" + weibo.getId(), null);
        /// 使用下面的方法会报错，不知道什么原因，待解。。。
        //int id = db.update(SqliteHelper.TB_NAME, values, UserBean.USER + "=" + user.getUser(), null);
        
        Log.i(TAG, "update " + id + "");
        
        return id;
    }
    
    /// 插入weibo表的记录
    public long insert(WeiboBean weibo)
    {
        ContentValues values = new ContentValues();

        values.put(WeiboBean.USER, weibo.getUser());
        
        ///头像
        if (weibo.getProfile() != null){
        	ByteArrayOutputStream profile = new ByteArrayOutputStream(); 
	        weibo.getProfile().compress(Bitmap.CompressFormat.JPEG, 50, profile);  
	        values.put(WeiboBean.PROFILE, profile.toByteArray());
        }
        
        if (weibo.getWeiboId() != 0){
        	values.put(WeiboBean.WEIBOID, weibo.getWeiboId());
        }
        if (weibo.getText() != null){
        	values.put(WeiboBean.TEXT, weibo.getText());
        }
        
        ///是否存在图片
        values.put(WeiboBean.EXISTICON, weibo.getExistImage()? 1: 0);
        
        ///图片
        if (weibo.getIcon() != null){
	        ByteArrayOutputStream icon = new ByteArrayOutputStream(); 
	        weibo.getIcon().compress(Bitmap.CompressFormat.JPEG, 50, icon);  
	        values.put(WeiboBean.ICON, icon.toByteArray());
        }
        
        ///是否存在GPS
        values.put(WeiboBean.EXISTLOCATION, weibo.getExistLocation()? 1: 0);

        ///GPS
        if (weibo.getExistLocation()) {
	        values.put(WeiboBean.LATITUDE, weibo.getLatitude());
	        values.put(WeiboBean.LONGITUDE, weibo.getLongitude());
        }
        
        ///创建日期
        if (weibo.getTime() != null){
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        values.put(WeiboBean.CREATEAT, sdf.format(weibo.getTime()));
        }
        
        long uid = db.insert(SqliteHelper4Weibo.TB_NAME, WeiboBean.ID, values);
        
        Log.i(TAG, "insert " + uid + "");
        
        return uid;
    }
    
    /// 删除weibo表的记录
    public int delete(long weiboId){
        int id = db.delete(SqliteHelper4Weibo.TB_NAME, WeiboBean.ID + "=" + weiboId, null);
        
        Log.i(TAG, "delete " + id + "");
        
        return id;
    }
}

